package com.atguigu.gmall.realtime.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Felix
 * @date 2022/9/26
 * 操作phoenix的工具类
 */
public class PhoenixUtil {
    //执行SQL语句的方法
    public static void executeSql(String sql) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //从连接池中获取连接对象
            conn = DruidDSUtil.getConnection();
            //获取数据库操作对象
            ps = conn.prepareStatement(sql);
            //执行SQL语句
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 从phoenix表中查询数据
     * @param sql 要执行的查询语句
     * @param clz 将查询的每一条结果封装为什么类型对象
     * @param <T>
     * @return
     */
    public static <T> List<T> queryList(String sql, Class<T> clz) {
        List<T> resList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //获取连接
            conn = DruidDSUtil.getConnection();
            //获取数据库操作对象
            ps = conn.prepareStatement(sql);
            //执行SQL语句
            rs = ps.executeQuery();
            //处理结果集
            // +-----+------------+
            // | ID  |  TM_NAME   |
            // +-----+------------+
            // | 1   | 三星        |
            // | 10  | 欧莱雅      |
            // | 11  | 香奈儿      |
            ResultSetMetaData metaData = rs.getMetaData();
            while(rs.next()){
                //创建对象  用于封装每条查询结果
                T obj = clz.newInstance();
                //获取表中的所有列
                for (int i = 1; i <=  metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    //给对象的属性赋值
                    BeanUtils.setProperty(obj,columnName,columnValue);
                }
                //将对象放到List集合中
                resList.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return resList;
    }

    public static void main(String[] args) {
        System.out.println(queryList("select * from GMALL0411_REALTIME.DIM_base_trademark", JSONObject.class));
    }
}
